package com.lzy.cloud.base.controller;

import com.lzy.cloud.base.dto.BaseStructureDto;
import com.lzy.cloud.base.request.BaseStructureRequest;
import com.lzy.cloud.base.response.BaseStructureResponse;
import com.lzy.cloud.base.service.StructureService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/structure")
@Slf4j
public class StructureController {

    @Autowired
    private StructureService structureService;

    /**
     * 列表
     * @param higherId
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result list(@RequestParam(value = "higherId") Long higherId) {
        List<BaseStructureDto> baseStructuredtoList = structureService.getListByParentId(higherId);
        Ensure.that(baseStructuredtoList).isNotNull("00010004");
        return new Result(DozerUtils.maps(baseStructuredtoList, BaseStructureResponse.class));
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public Result detail(@RequestParam(value = "id") Long id) {
        BaseStructureDto baseStructuredto = structureService.findById(id);
        Ensure.that(baseStructuredto).isNotNull("00010003");
        return new Result(DozerUtils.map(baseStructuredto, BaseStructureResponse.class));
    }

    /**
     * 新增
     * @param baseStructureRequest
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody BaseStructureRequest baseStructureRequest) {
        Long ok = structureService.insert(DozerUtils.map(baseStructureRequest, BaseStructureDto.class));
        Ensure.that(ok > 0).isTrue("00010001");
        return new Result();
    }

    /**
     * 修改
     * @param baseStructureRequest
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody BaseStructureRequest baseStructureRequest) {
        int ok = structureService.update(DozerUtils.map(baseStructureRequest, BaseStructureDto.class));
        Ensure.that(ok > 0).isTrue("00010002");
        return new Result();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    public Result del(@RequestParam Long id) {
        int ok = structureService.deleteById(id);
        Ensure.that(ok > 0).isTrue("00010005");
        return new Result();
    }

    /**
     * 检查子集存在
     * @param id
     * @return
     */
    @RequestMapping(value = "/check/child",method = RequestMethod.GET)
    public Result checkChild(@RequestParam Long id) {
        int ok = structureService.getListCountByParentId(id);
        Ensure.that(ok > 0).isTrue("00010006");
        return new Result(ok);
    }

}